#include <vector>

using namespace std;

class Solution {
public:
    int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {
        bool need_next;
        do {
            need_next = false;
            for (int i = edges.size() - 1; i >= 0; i--) {
                if (hasApple[edges[i][1]] && !hasApple[edges[i][0]]) {
                    need_next = true;
                    hasApple[edges[i][0]] = true;
                }
            }
        } while (need_next);
        int ans = 0;
        for (vector<int>& edge : edges) {
            if (hasApple[edge[1]]) {
                ans += 2;
            }
        }
        return ans;
    }
};